﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using QuanLyBanHang.QuerySQL;

namespace QuanLyBanHang
{
    public partial class frThongkesp : Office2007Form
    {
        ClassTKSP obj = new ClassTKSP();
        int m, d, y;
        public frThongkesp()
        {
            InitializeComponent();
        }

        private void frThongkesp_Load(object sender, EventArgs e)
        {
            this.AcceptButton = btnthongke;
            dtgv_CTHD.DataSource = obj.ShowCTHD(0);
        }

        private void dtgv_CTHD_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
        {
            try
            {
                for (int i = 0; i < dtgv_CTHD.RowCount; i++)
                {
                    dtgv_CTHD.Rows[i].Cells[0].Value = i + 1;
                }
            }
            catch { }
        }

        private void cbosanpham_Click(object sender, EventArgs e)
        {
            cbosanpham.DataSource = obj.ShowCTHD(1);
            cbosanpham.ValueMember = "MASP";
            cbosanpham.DisplayMember = "MASP";
        }

        private void cbosanpham_TextChanged(object sender, EventArgs e)
        {
            dtgv_CTHD.DataSource = obj.ShowCTHD(2,cbosanpham.Text);
        }

        private void txtthang_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
                e.Handled = true;
        }
        private void txtthang_TextChanged(object sender, EventArgs e)
        {
            try
            {
                m = Convert.ToInt32(txtthang.Text);
            }
            catch { }
        }

        private void txtngay_TextChanged(object sender, EventArgs e)
        {
            try
            {
                d = Convert.ToInt32(txtngay.Text);
            }
            catch { }
        }

        private void txtnam_TextChanged(object sender, EventArgs e)
        {
            try
            {
                y = Convert.ToInt32(txtnam.Text);
            }
            catch { }
        }

        private void btnthongke_Click(object sender, EventArgs e)
        {
            if (cbosanpham.Text.Length == 0)
            {
                lbthongbao.Text = "Nhập mã hoặc tên sp...";
                cbosanpham.Focus();

            }
            else if (cboloai.SelectedIndex == 0)
            {
                if (txtngay.TextLength == 0)
                {
                    lbthongbao.Text = "Nhập ngày...";
                    txtngay.Focus();
                }
                else if (txtthang.TextLength == 0)
                {
                    lbthongbao.Text = "Nhập tháng...";
                    txtthang.Focus();
                }
                else if (txtnam.TextLength == 0)
                {
                    lbthongbao.Text = "Nhập năm...";
                    txtnam.Focus();
                }
                else
                {
                    lbthongbao.Text = "";
                    lbdate.Text = txtngay.Text + " / " + txtthang.Text + " / " + txtnam.Text;
                    foreach (DataRow item in obj.ShowCTHD(3, cbosanpham.Text).Rows)
                    {
                        lbten.Text = item["TENSP"].ToString();
                        lbgia.Text = item["GIAMUA"].ToString();
                        lbslg.Text = item["SOLUONG"].ToString();
                    }
                    foreach (DataRow item in obj.TKSL(0,cbosanpham.Text,y,m,d).Rows)
                    {
                        lbdb.Text = item["SL"].ToString();
                    }
                    
                }
            }
            else if (cboloai.SelectedIndex == 1)
            {
                if (txtthang.TextLength == 0)
                {
                    lbthongbao.Text = "Nhập tháng...";
                    txtthang.Focus();
                }
                else if (txtnam.TextLength == 0)
                {
                    lbthongbao.Text = "Nhập năm...";
                    txtnam.Focus();
                }
                else
                {
                    txtngay.Clear();
                    lbthongbao.Text = "";
                    lbdate.Text = txtthang.Text + " / " + txtnam.Text;
                    foreach (DataRow item in obj.TKSL(1, cbosanpham.Text, y, m).Rows)
                    {
                        lbdb.Text = item["SL"].ToString();
                    }
                    foreach (DataRow item in obj.ShowCTHD(3, cbosanpham.Text).Rows)
                    {
                        lbten.Text = item["TENSP"].ToString();
                        lbgia.Text = item["GIAMUA"].ToString();
                        lbslg.Text = item["SOLUONG"].ToString();
                    }
                }
            }
            else if (cboloai.SelectedIndex == 2)
            {
                if (txtnam.TextLength == 0)
                {
                    lbthongbao.Text = "Nhập năm...";
                    txtnam.Focus();
                }
                else
                {
                    txtngay.Clear();
                    txtthang.Clear();
                    lbthongbao.Text = "";
                    lbdate.Text = txtnam.Text;
                    foreach (DataRow item in obj.TKSL(2, cbosanpham.Text, y).Rows)
                    {
                        lbdb.Text = item["SL"].ToString();
                    }
                    foreach (DataRow item in obj.ShowCTHD(3, cbosanpham.Text).Rows)
                    {
                        lbten.Text = item["TENSP"].ToString();
                        lbgia.Text = item["GIAMUA"].ToString();
                        lbslg.Text = item["SOLUONG"].ToString();
                    }
                }
            }
            else
            {
                lbthongbao.Text = "Chon loại thống kê...";
                cboloai.Focus();
            }
        }

        private void cboloai_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cboloai.SelectedIndex == 0)
            {
                txtngay.Enabled = true;
                txtthang.Enabled = true;
                txtnam.Enabled = true;
            }
            else if (cboloai.SelectedIndex == 1)
            {
                txtngay.Enabled = false;
                txtthang.Enabled = true;
                txtnam.Enabled = true;
            }
            if (cboloai.SelectedIndex == 2)
            {
                txtngay.Enabled = false;
                txtthang.Enabled = false;
                txtnam.Enabled = true;
            }
        }

        private void btnthoat_Click(object sender, EventArgs e)
        {
            cbosanpham.Text = "";
            cboloai.SelectedIndex = 0;
            txtngay.Clear();
            txtthang.Clear();
            txtnam.Clear();
            lbdate.Text = "*";
            lbslg.Text = "*";
            lbdb.Text = "*";
            lbten.Text = "*";
            lbgia.Text = "*";
        }
    }
}
